{% set pypi_name = 'murano' %}
{% set source = fetch_source('https://tarballs.openstack.org/murano/murano-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
%global with_doc 1
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
Name:           {{ py2name() }}
Epoch:          {{ epoch('murano') }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        OpenStack Application Catalog (murano)
License:        {{ license('Apache-2.0') }}
Group:          System/Management
URL:            https://docs.openstack.org/murano
Source0:        {{ source | basename }}
Source1:        openstack-murano.logrotate
Source2:        openstack-murano.tmpfiles
Source3:        openstack-murano.defaultconf
Source4:        openstack-murano.README.config
# systemd service files
Source5:        openstack-murano-api.service
Source6:        openstack-murano-engine.service
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('Babel') }}
BuildRequires:  {{ py3('WebOb') }}
BuildRequires:  {{ py3('castellan') }}
BuildRequires:  {{ py3('fixtures') }}
BuildRequires:  {{ py3('python-heatclient') }}
BuildRequires:  {{ py3('jsonschema') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('python-mistralclient') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('python-muranoclient') }}
BuildRequires:  {{ py3('python-neutronclient') }}
BuildRequires:  {{ py3('oslo.concurrency') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.i18n') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.messaging') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslo.upgradecheck') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('psutil') }}
BuildRequires:  {{ py3('requests') }}
BuildRequires:  {{ py3('semantic_version') }}
BuildRequires:  {{ py3('six') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('tenacity') }}
BuildRequires:  {{ py3('yaql') }}
Requires:       logrotate
Requires:       python3-{{ pypi_name }} = %{epoch}:%{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
Requires:       pwdutils
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(pre):  shadow-utils
Requires(preun): systemd
%endif

%description
OpenStack Application Catalog.

%package -n python3-murano
Summary:        OpenStack Application Catalog (murano) - Python module
Group:          Development/Languages/Python
Requires:       {{ py3('Babel') }}
Requires:       {{ py3('Paste') }}
Requires:       {{ py3('PasteDeploy') }}
Requires:       {{ py3('PyYAML') }}
Requires:       {{ py3('Routes') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('WebOb') }}
Requires:       {{ py3('alembic') }}
Requires:       {{ py3('castellan') }}
Requires:       {{ py3('debtcollector') }}
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('python-heatclient') }}
Requires:       {{ py3('jsonpatch') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('kombu') }}
Requires:       {{ py3('python-mistralclient') }}
Requires:       {{ py3('python-muranoclient') }}
Requires:       {{ py3('netaddr') }}
Requires:       {{ py3('python-neutronclient') }}
Requires:       {{ py3('oslo.concurrency') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.context') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.i18n') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.messaging') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.policy') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('psutil') }}
Requires:       {{ py3('semantic_version') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('stevedore') }}
Requires:       {{ py3('tenacity') }}
Requires:       {{ py3('testtools') }}
Requires:       {{ py3('yaql') }}

%description -n python3-murano
OpenStack Application Catalog.

This package contains the core Python module of OpenStack murano.

%package api
Summary:        OpenStack Application Catalog (murano) - API
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description api
OpenStack Application Catalog.

This package contains the OpenStack murano API.

%package engine
Summary:        OpenStack Application Catalog (murano) - API
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description engine
OpenStack Application Catalog.

This package contains the OpenStack murano Engine.

%if 0%{?with_doc}
%package doc
Summary:        Openstack Application Catalog (murano) - Documentation
Group:          Documentation/HTML
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('openstackdocstheme') }}
BuildRequires:  {{ py3('reno') }}
BuildRequires:  {{ py3('sphinxcontrib-httpdomain') }}

%description doc
This package contains the documentation for OpenStack Murano.
%endif

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup

%build
%{py3_build}

# documentation
%if 0%{?with_doc}
PYTHONPATH=. PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
PYTHONPATH=. PBR_VERSION={{ upstream_version }} %sphinx_build -b man doc/source doc/build/man
# remove the Sphinx-build leftovers
rm -rf doc/build/html/.{doctrees,buildinfo}
rm -rf doc/build/man/.{doctrees,buildinfo}
%endif

%install
%{py3_install}

### directories
install -d -m 750 %{buildroot}%{_localstatedir}/{lib,log,cache}/murano
install -D -m 644 %{SOURCE2} %{buildroot}/%{_tmpfilesdir}/%name.conf
install -d -m 755 %{buildroot}%{_sysconfdir}/murano
install -d -m 755 %{buildroot}%{_sysconfdir}/murano/murano.conf.d/

# sample config files
PYTHONPATH=. oslo-config-generator --config-file etc/oslo-config-generator/murano.conf --output-file %{buildroot}%{_sysconfdir}/murano/murano.conf
PYTHONPATH=. oslo-config-generator --config-file etc/oslo-config-generator/murano-cfapi.conf --output-file %{buildroot}%{_sysconfdir}/murano/murano-cfapi.conf

### configuration files
install -p -D -m 640 %{SOURCE4} %{buildroot}%{_sysconfdir}/murano/README.config
install -p -D -m 640 %{SOURCE3} %{buildroot}%{_sysconfdir}/murano/murano.conf.d/010-murano.conf
mv %{buildroot}%{_prefix}%{_sysconfdir}/murano/* %{buildroot}%{_sysconfdir}/murano/

# bash-completion/logrotate/etc.
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}

### systemd service files
install -p -D -m 444 %{SOURCE5} %{buildroot}%{_unitdir}/%{name}-api.service
install -p -D -m 444 %{SOURCE6} %{buildroot}%{_unitdir}/%{name}-engine.service

# install symlinks on SUSE systems
%if 0%{?suse_version}
mkdir -p %{buildroot}%{_sbindir} %{buildroot}%{_unitdir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-api
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-engine
%endif

# man pages
%if 0%{?with_doc}
mkdir -p %{buildroot}%{_mandir}/man1
install -p -D -m 644 doc/build/man/*.1 %{buildroot}%{_mandir}/man1/
%endif

%check
# don't want to depend on hacking for package building
rm murano/tests/unit/test_hacking.py
%{__python3} -m stestr.cli run

%pre
%openstack_pre_user_group_create murano murano /sbin/nologin

%post
%tmpfiles_create %{_tmpfilesdir}/murano.conf

%post api
%systemd_post %{name}-api.service

%preun api
%systemd_preun %{name}-api.service

%postun api
%systemd_postun_with_restart %{name}-api.service

%post engine
%systemd_post %{name}-engine.service

%preun engine
%systemd_preun %{name}-engine.service

%postun engine
%systemd_postun_with_restart %{name}-engine.service

%files
%doc README.rst
%license LICENSE
%dir %attr(0750, murano, murano) %{_localstatedir}/lib/murano
%dir %attr(0750, murano, murano) %{_localstatedir}/cache/murano
%dir %attr(0750, murano, murano) %{_localstatedir}/log/murano
%{_tmpfilesdir}/%{name}.conf
%dir %{_sysconfdir}/murano
%dir %{_sysconfdir}/murano/murano.conf.d/
%{_sysconfdir}/murano/README.config
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%config(noreplace) %attr(0640, root, murano) %{_sysconfdir}/murano/murano.conf
%config(noreplace) %attr(0640, root, murano) %{_sysconfdir}/murano/murano.conf.d/010-murano.conf
%config(noreplace) %attr(0640, root, murano) %{_sysconfdir}/murano/murano-cfapi.conf
%config %{_sysconfdir}/murano/murano-paste.ini
%config %{_sysconfdir}/murano/murano-cfapi-paste.ini
%{_bindir}/murano-manage
%{_bindir}/murano-db-manage
%{_bindir}/murano-status
%{_bindir}/murano-test-runner
%if 0%{?with_doc}
%{_mandir}/man1/murano.1.gz
%endif

%files -n python3-murano
%license LICENSE
%{python3_sitelib}/murano/
%{python3_sitelib}/murano-*.egg-info

%if 0%{?with_doc}
%files doc
%license LICENSE
%doc doc/build/html
%endif

%files api
%license LICENSE
%{_unitdir}/%{name}-api.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-api
%endif
%{_bindir}/murano-api
%{_bindir}/murano-cfapi
%{_bindir}/murano-cfapi-db-manage
%{_bindir}/murano-wsgi-api

%files engine
%license LICENSE
%{_unitdir}/%{name}-engine.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-engine
%endif
%{_bindir}/murano-engine

%changelog
